const mysql2 = require('mysql2')
const config = require('./config')


const connectionPool = mysql2.createPool(config)

/**
 * @class DataBase
 * @description 创建数据库基础配置
 */
class DataBase {
    constructor() {
        this.init()
    }

    handleErr = err => console.log(err.message)

    init() {
        connectionPool.getConnection((err, connection1) => {
            if (err) return this.handleErr(err)
            connection1.connect(err1 => console.log(err1 ? this.handleErr(err1) : '数据库连接成功！'))
        })
    }

    /**
     *
     * @param statement {string} sql语句 支持 ？ ？
     * @return {Query}
     */
    //性能高 但不支持问好写法
    exec = statement => connectionPool.promise().execute(statement)
    //支持问号写法 但耗性能
    query = statement => connectionPool.promise().query(statement)

}


module.exports = database = new DataBase()

